package com.soen6441.generics;
class CovariantArrays {
    static class A {};
    static class B extends A {};
    static class C extends B {};

    static int count(Object[] objects){
        int result = 0;
        for(Object object : objects)
            result++;
        return result;
    }

    static void copy(Object[] source, Object[] destination){
        for(int i = 0; i < source.length; i++){
            destination[i] = source[i];
        }
    }

    public static void main(String[] args) {
        A[] as = {new A(), new A(), new A()};
        B[] emptyBs = new B[as.length];

        B[] bs = {new B(), new B(), new B()};
        A[] emptyAs = new A[bs.length];

        // compiles and runs B is an A
        copy(bs, emptyAs);

        // compiles but blows up at runtime
        // ArrayStoreException
        // A is not a B
        copy(as, emptyBs);
        // no way to turn off covariance
        // thats why it is favorable to
        // use generic collections
    }
}
